<?php
/**
 * adv
 * @version 1.0
 * @package application
 * @subpackage application/controllers/adv/
 */
class Adv extends MY_Controller
{	
	public function __construct(){
		parent::__construct();
		$this->_rule_config=array(array('field'=> 'adv_id' , 'label'=> '广告自增标识编号' , 'rules'=> 'required'),
			array('field'=> 'ap_id' , 'label'=> '广告位id' , 'rules'=> 'required'),
			array('field'=> 'adv_title' , 'label'=> '广告内容描述' , 'rules'=> 'required'),
			array('field'=> 'adv_content' , 'label'=> '广告内容' , 'rules'=> 'required'),
			array('field'=> 'adv_start_date' , 'label'=> '广告开始时间' , 'rules'=> 'required'),
			array('field'=> 'adv_end_date' , 'label'=> '广告结束时间' , 'rules'=> 'required'),
			array('field'=> 'slide_sort' , 'label'=> '幻灯片排序' , 'rules'=> 'required'),
			array('field'=> 'click_num' , 'label'=> '广告点击率' , 'rules'=> 'required'),
			array('field'=> 'created' , 'label'=> '添加时间' , 'rules'=> 'required'),
			array('field'=> 'modified' , 'label'=> '修改时间' , 'rules'=> 'required'),
			array('field'=> 'status' , 'label'=> '状态' , 'rules'=> 'required'),
			);

		$this->load->model('adv_model');
	}
	/**
	 * index 显示列表页
	 * @author 
	 */	
	public function index(){
		$attributes=$this->input->get();
		$search['attributes']=$attributes;
		// 总数与分页
		$total = $this->adv_model->total($search);
		$data['total'] = $total;
		// 请使用config_item( 'per_page' )获取全局显示条数
		$per_page = config_item( 'per_page' );
		$this->load->library('pagination');
		$pagination_config = array(
			'base_url' => base_url($this->_site_path.'/adv/index' ),
			'total_rows' => $total,
			'per_page' => $per_page,
			'uri_segment' => 4,
		);
		$this->pagination->initialize( $pagination_config );
		$data['pagination'] = $this->pagination->create_links();

		$search['limit']=array('persize'=>$per_page,'offset'=>$this->pagination->get_cur_offset());
		$data['adv_data']=$this->adv_model->all($search);
		$this->load->view($this->_site_path.'/adv/index',$data);
	}

	/**
	 * view 显示详细页
	 * @param $id 主键值
	 * @author 
	 */
	public function view($id)
	{
		$data['adv_data']=$this->adv_model->get_by_pk($id);
		$this->load->view($this->_site_path.'/adv/view',$data);
	}

	/**
	 * add 添加 
	 * @author 
	 */
	public function add()
	{
		$data = array( );

		if ( $this->input->post( 'is_submit' ) && $this->form_validation->check_token() ){
			try {
				$adv_data = $this->input->post();
				$data['adv_data'] = $adv_data;
				//设置验证规则
				$this->form_validation->set_rules($this->_rule_config);
				if ( !$this->form_validation->run() ) throw new Exception( validation_errors(), 0 );
				//插入
				$adv_id = $this->adv_model->insert( $adv_data );
				init_messagebox( '添加成功', 'success', 1, base_url( $this->_site_path.'/adv/index' ) );
			} catch ( Exception $e ) {
				init_messagebox( $e->getMessage(), 'error', $e->getCode() );
			}
		}
		$this->load->view($this->_site_path.'/adv/form', $data );
	}

	/**
	 * edit 编辑 
	 * @param $id 主键值
	 * @author 
	 */
	public function edit($id)
	{
		$data = array( );
		$data['adv_data']=$this->adv_model->get_by_pk($id);
		if ( $this->input->post( 'is_submit' ) && $this->form_validation->check_token() ){
			try {
				$adv_data = $this->input->post();
				$data['adv_data'] = $adv_data;
				//设置验证规则
				$this->form_validation->set_rules($this->_rule_config);
				if ( !$this->form_validation->run() ) throw new Exception( validation_errors(), 0 );
				//修改
				$this->adv_model->update_by_pk( $adv_data ,$id);
				init_messagebox( '修改成功', 'success', 1, base_url( $this->_site_path.'/adv/index' ) );
			} catch ( Exception $e ) {
				init_messagebox( $e->getMessage(), 'error', $e->getCode() );
			}
		}
		$this->load->view($this->_site_path.'/adv/form', $data );
	}	

	/**
	 * delete 删除
	 * @param $id 主键值
	 * @author 
	 */
	public function delete( $id ) {
		$this->adv_model->delete_by_pk( $id );
		redirect( base_url($this->_site_path.'/adv/index' ) );
	}

	/**
	 * delete_all 批量删除
	 * @param $id 主键值
	 * @author 
	 */
	public function delete_all() {
		$ids=$this->input->post('ids');
		foreach($ids as $id){
			$this->adv_model->delete_by_pk( $id );
		}
		redirect( base_url($this->_site_path.'/adv/index' ) );
	}
}
